﻿#include "BooleanEditEntry.h"
#include <QPointer>
#include <QCheckBox>

struct BooleanEditEntry::BooleanEditEntryPrivate {
    QPointer<QCheckBox> checkBox;
};

BooleanEditEntry::~BooleanEditEntry() { }

BooleanEditEntry::BooleanEditEntry(QWidget *parent) : EditEntry{parent} {
    p.reset(new BooleanEditEntryPrivate);
    p->checkBox = new QCheckBox;
    addEditControll(p->checkBox);

    connect(p->checkBox.data(), &QCheckBox::clicked, this, &BooleanEditEntry::onEditingFinished);
}

void BooleanEditEntry::setValue(bool checked) {
    if(!p->checkBox->hasFocus()) {
        p->checkBox->setChecked(checked);
    }
}

bool BooleanEditEntry::value() const {
    return p->checkBox->isChecked();
}

void BooleanEditEntry::onEditingFinished() {
    emit valueChanged(p->checkBox->isChecked());
    clearFocus();
}
